package class1;

import java.util.Arrays;

/**
 * 插入排序
 */
public class Code05_InsertSort {
    public static void main(String[] args) {
        int[] arr = new int[]{2, 5, 1, 9, 12, 6, 7, 3, 1};
        insertSort(arr);
        System.out.println(Arrays.toString(arr));

    }

    private static void insertSort(int[] arr) {
        for (int i = 1; i < arr.length; i++) {
            int cur = arr[i], j = i;
            while (j > 0 && arr[j - 1] > cur) {
                arr[j] = arr[j - 1]; //相当于右移
                j--;
            }
            arr[j] = cur;
        }
    }

}
